package com.api.base.service.impl;

import com.api.base.config.RoleEnum;
import com.api.base.dao.SysRoleMapper;
import com.api.base.dao.SysUserRoleMapper;
import com.api.base.dao.UserMapper;
import com.api.base.model.SysRole;
import com.api.base.model.User;
import com.api.base.service.UserService;
import com.api.core.service.AbstractService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/03/17.
 */
@Service
@Transactional
public class UserServiceImpl extends AbstractService<User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = userMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    @Override
    @Cacheable(cacheNames = "role", key = "#userId")
    public List<SysRole> getRole(Long userId) {
        return sysRoleMapper.getByUser(userId, RoleEnum.API.getType());
    }

    @Override
    public Result updatePassword(Long userId, String oldpassword, String password) {
        User user = this.findById(userId);
        if(!new BCryptPasswordEncoder().matches(oldpassword, user.getPassword())){
            return ResultGenerator.genFailResult(400,"Old password is wrong");
        }
        password = new BCryptPasswordEncoder().encode(password);


        user.setPassword(password);

        this.update(user);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result transactionPasswordUpdate(Long userId, String oldpassword, String password) {
        User user = this.findById(userId);

        if(StringUtils.isNotEmpty(user.getTransactionPassword())){
            if(!new BCryptPasswordEncoder().matches(oldpassword, user.getTransactionPassword())){
                return ResultGenerator.genFailResult(400,"Old password is wrong");
            }
        }
        password = new BCryptPasswordEncoder().encode(password);


        user.setTransactionPassword(password);

        this.update(user);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Map<String, Object> info(Long id) {
        return userMapper.info(id);
    }

    @Override
    public List<SysRole> getMenu(Long userId) {
        return sysRoleMapper.getByUser(userId, RoleEnum.MENU.getType());
    }

    @Override
    public List<Map<String, Object>> registerAnalysis() {
        return userMapper.registerAnalysis();
    }
}
